package com.example.demo.controller;

import com.example.demo.common.AjaxResult;
import com.example.demo.common.ApplicationVal;
import com.example.demo.common.PasswordUtils;
import com.example.demo.common.UserSessionUtils;
import com.example.demo.entity.UserInfo;
import com.example.demo.entity.vo.ArticleInfoVo;
import com.example.demo.service.ArticleSevice;
import com.example.demo.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.xml.ws.RequestWrapper;

/**
 * Description:
 * User: 故我
 * Date: 2023-11-08
 * Time: 12:39
 */
@RestController  // Controller + ResponseBody
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @Autowired
    private ArticleSevice articleSevice;

    private ImagCodeController imagCodeController;

    //返回统一类型的数据格式
    @RequestMapping("/register")
    public AjaxResult register(UserInfo userInfo) {
        //判断前端传递的数据，进行数据校验
        if (userInfo == null || !StringUtils.hasLength(userInfo.getUsername())
                || !StringUtils.hasLength(userInfo.getPassword())) {
            //此时前端传递的数据异常
            return AjaxResult.fail(-1, "非法的参数异常！");
        }
        // 设置用户的加盐密码到数据库中
        userInfo.setPassword(PasswordUtils.SaltPassword(userInfo.getPassword()));
        return AjaxResult.success(userService.register(userInfo));
    }

    @RequestMapping("/login")
    public AjaxResult login(HttpServletRequest request, String username, String password, String imagcode) {
        //进行非空校验  账号密码有一个错误就返回错误响应
        if (!StringUtils.hasLength(username) || !StringUtils.hasLength(password) || !StringUtils.hasLength(imagcode)) {
            return AjaxResult.fail(-1, "非法的参数异常！");
        }
        //需要去数据库中读取用户信息
        UserInfo userInfo = userService.login(username);
        if (userInfo != null && userInfo.getId() > 0) {
            // 这个是一个有效的用户，判断密码是否一致  普通密码 和 数据库最终密码
            if (PasswordUtils.CheckPassword(password, userInfo.getPassword())) {
                // 将用户信息返回给前端 并将密码隐藏  并将用户的信息保存在session中
                HttpSession session = request.getSession(true); //没有session值，自动创建一个session对象
                session.setAttribute(ApplicationVal.USER_SESSION_KEY, userInfo); // key value
                userInfo.setPassword("");
            }
            // 需要判断验证码是否输入正确
            if (!imagcode.equals(imagCodeController.lineCaptcha.getCode())) {
                return AjaxResult.fail(-2, "错误验证码!");
            }
            return AjaxResult.success(userInfo);
        }
        // 用户信息获取失败，前端传入信息有误
        return AjaxResult.success(0, null);
    }

    @RequestMapping("/showinfo")
    public AjaxResult showinfo(HttpServletRequest request) {
        // 从请求session中获取到请求的用户信息,前面存储的就是userinfo信息
        ArticleInfoVo articleInfoVo = new ArticleInfoVo();
        UserInfo userInfo = UserSessionUtils.getUser(request);
        // 判断session中是否存在user信息
        if (userInfo == null) {
            return AjaxResult.fail(-1, "非法请求!");
        }
        // 使用spring 提供的深拷贝接口
        BeanUtils.copyProperties(userInfo, articleInfoVo);//src desc
        articleInfoVo.setArtCount(articleSevice.getArtCountByUid(userInfo.getId()));
        return AjaxResult.success(articleInfoVo);
    }

    @RequestMapping("/userdel")
    public AjaxResult userdel(HttpSession session) {
        // 直接删除session中的key值
        session.removeAttribute(ApplicationVal.USER_SESSION_KEY);
        return AjaxResult.success(1);//data
    }

    @RequestMapping("/getuserbyid")
    public AjaxResult getUserById(Integer id) {
        // 非空校验 id
        if (id == null || id <= 0) {
            return AjaxResult.fail(-1, "非法参数!");
        }
//        UserInfo userInfo = UserSessionUtils.getUser(request);
        UserInfo userInfo = userService.getUserById(id);
        if (userInfo == null || userInfo.getId() <= 0) {
            return AjaxResult.fail(-1, "非法参数!");
        }
        // 对私密数据进行过滤
        userInfo.setPassword("");
        ArticleInfoVo articleInfoVo = new ArticleInfoVo();
        BeanUtils.copyProperties(userInfo, articleInfoVo);
        articleInfoVo.setArtCount(articleSevice.getArtCountByUid(id));
        return AjaxResult.success(articleInfoVo);
    }

//    @RequestMapping("/getheadphoto")
//    public
}
